﻿#include "MainMenuScene.h"

#include <cocostudio/CocoStudio.h>
using namespace cocostudio;
bool MainMenuScene::init()

{
	//////////////////////////////
	// 1. super init first
	if (!Layer::init())
	{
		return false;
	}

	m_sceneLayout = CSLoader::getInstance()->createNode("asset/MainScene.csb");

	float scale = Director::getInstance()->getContentScaleFactor();
	this->addChild(m_sceneLayout);


	Button *button1 = (Button*)m_sceneLayout->getChildByName("Button_1");
	//EventListener* listener = EventListenerCustom::create("aaaa", [this](EventCustom* e){ /* do something */ });
	//button1->addClickEventListener(listener);
	//button1->addClickEventListener(CC_CALLBACK_2(MainMenuScene::doSomething, this));
	button1->setTouchEnabled(true);
	button1->addTouchEventListener(this, toucheventselector(MainMenuScene::touchEvent));
	return true;
}

cocos2d::Scene* MainMenuScene::createScene()
{

	// 'scene' is an autorelease object
	auto scene = Scene::create();

	// 'layer' is an autorelease object
	auto layer = MainMenuScene::create();

	// add layer as a child to scene
	scene->addChild(layer);

	// return the scene
	return scene;
}

void MainMenuScene::doSomething()
{
	//Node *p = (Node*)pSender;
}

void MainMenuScene::touchEvent(Ref *pSender, TouchEventType type)
{
	ui::Widget::TouchEventType uiType = (ui::Widget::TouchEventType)type;
		
	ui::Text *txt = (Text*)m_sceneLayout->getChildByName("Text_1");

	switch (uiType)
	{
	case ui::Widget::TouchEventType::
	BEGAN:
	{
		txt->setText("UIButton Click.");
	}break;
		
	case ui::Widget::TouchEventType::
	MOVED:
	{

	}break;

	case ui::Widget::TouchEventType::
	ENDED:
	{
		txt->setText("UIButton Release.");
	} break;

	case ui::Widget::TouchEventType::
	CANCELED:
	{
		txt->setText("UIButton Cancel.");

	} break;

	default:
		// TODO
		break;
	}
}